/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.dao.impl;

import com.opgea.qms.dao.CompanyDAO;
import com.opgea.qms.domain.entities.Company;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ramesh
 */
@Repository
@Transactional
public class CompanyDAOImpl implements CompanyDAO{

    
    //private Session session = HibernateUtil.getSessionFactory().openSession();
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public Company create(Company company){
        sessionFactory.openSession().save(company);
        return company;
    }

    @Override
    public Company update(Company company) {
        sessionFactory.openSession().update(company);
        return company;
    }

    @Override
    public Company delete(Long companyId) {
        Company company = this.find(companyId);
        sessionFactory.openSession().delete(company);
        return company;
    }

    @Override
    public Company find(Long id) {
        Query query = sessionFactory.openSession().createQuery("from Company c where c.id = "+id);
        Company company = (Company) query.uniqueResult();
        return company;
    }

    @Override
    public List<Company> findAll() {
        List<Company> companies = sessionFactory.openSession().createQuery("from Company").list();
        return companies;
    }
    
}
